#!/bin/bash
set -e
#set $HOME
if [[ ! -d $HOME ]]; then
    HOME=~/
fi
if [[ ! -d $HOME ]]; then
    echo ' ERROR: $HOME varialbe should point to /home/user'
    exit 1
fi

show_help(){ cat <<EOF

    Configure options:
    --prefix=/usr           -p   Look for libraries starting in /usr
    --libdir=/lib           -l   Look for libraries in \$PREFIX/lib
    --winelib=/opt/wine     -w   Location of libwinecrt0.a
    --with-64bit            -64  Compile as 64 bit
    --xincdir=/opt/include  -x   Optional includes in /opt/include
    --help                  -h   This help message
EOF
}

bitness(){
    if [[ $is64 ]]; then
        echo "64 bit"
    else
        echo "32 bit"
    fi
    echo " or try one of these ./configure options:"
}

# get options
while [[ $1 == -* ]]; do
    case "$1" in
        -h|--help|-\?) show_help; exit 0;;
        -64|--with-64bit) is64=1; shift;;
        -l|--libdir) if (($# > 1)); then
                LIBDIR=$2; shift 2
            else
                echo "-l requires an argument" 1>&2
                exit 1
            fi ;;
        --libdir=/*) LIBDIR=${1##*=}; shift;;
        -p|--prefix) if (($# > 1)); then
                PREFIX=$2; shift 2
            else
                echo "-p requires an argument" 1>&2
                exit 1
            fi ;;
        --prefix=/*) PREFIX=${1##*=}; shift;;
        -w|--winelib) if (($# > 1)); then
                WINELIBDIR=$2; shift 2
            else
                echo "-W requires an argument" 1>&2
                exit 1
            fi ;;
        --winelib=/*) WINELIBDIR=${1##*=}; shift;;
        -x|--xincdir) if (($# > 1)); then
                XINCDIR=$2; shift 2
            else
                echo "-W requires an argument" 1>&2
                exit 1
            fi ;;
        --xincdir=/*) XINCDIR=${1##*=}; shift;;
        --) shift; break;;
        -*) echo "invalid option: $1" 1>&2; show_help; exit 1;;
    esac
done

# test compiler
printf "Looking for winegcc...  "
if [[ ! $(which winegcc 2> /dev/null) ]]; then
    echo Development files for wine not installed!
    echo Install wine-devel and try again.
    exit 1
fi; echo "(OK)"

# test WINEPREFIX
printf "Checking WINEPREFIX...  "
if [[ ! -d $WINEPREFIX ]]; then
    if [[ ! -d $HOME/.wine ]]; then
        echo "WINEPREFIX not set and .wine folder doesn't exist"
        echo "export WINEPREFIX= and run winecfg. See README."
        exit 1
    else # wine exists
        cat <<EOF

WARNING: Installing to $HOME/.wine is deprecated.
You may continue, but it is better to export
WINEPREFIX to some other directory and re-run configure.
Install dictation software there, too. See README.
EOF
        WINEPREFIX=$HOME/.wine
    fi
fi; echo "(OK)"

# automatically set /prefix/libdir if not set
printf "Checking LIBDIR...      "
if [[ ! $PREFIX ]]; then
    PREFIX=/usr
fi
if [[ ! $LIBDIR ]]; then
    if [[ $is64 ]]; then
        if [[ -d ${PREFIX}/lib32 ]]; then
            LIBDIR=${PREFIX}/lib
        else
            LIBDIR=${PREFIX}/lib64
        fi
    else
        if [[ -d ${PREFIX}/lib ]]; then
            LIBDIR=${PREFIX}/lib
        else
            LIBDIR=${PREFIX}/lib32
        fi
    fi
else #libdir is set
    if [[ $PREFIX ]]; then
        LIBDIR=${PREFIX}${LIBDIR}
    fi
fi
# fix up
LIBDIR=${PREFIX}${LIBDIR##*${PREFIX}}
echo "$LIBDIR"

# check wine library linkage
printf "Checking winelib...     "
if [[ ! $WINELIBDIR ]]; then
    WINELIBDIR="${LIBDIR}/wine"
fi
if [[ $LIBDIR ]]; then
    if [[ ! -f "${WINELIBDIR}/libwinecrt0.a" ]]; then
        echo "${WINELIBDIR}/libwinecrt0.a not found!"
        printf "\n ERROR: Install wine-devel ";bitness
        show_help
        exit 1
    fi
fi; echo "(OK)"

# set includdir
if [[ ! $XINCDIR ]]; then
    XINCDIR=/usr/include
fi

# check xorg proto
printf "Checking xorg-proto...  "
if [[ ! -f "${XINCDIR}/X11/Xdefs.h" ]]; then
    echo "${XINCDIR}/X11/Xdefs.h not found!"
    printf "\n ERROR: Install xorg-x11-proto-devel"
    show_help
    exit 1
fi; echo "(OK)"

# check libX11-devel
printf "Checking libX11...      "
if [[ ! -f "${LIBDIR}/libX11.so" ]]; then
    echo "${LIBDIR}/libX11.so not found!"
    printf "\n ERROR: Install libX11-devel ";bitness
    show_help
    exit 1
fi; echo "(OK)"

# check libXtst-devel
printf "Checking libXtst...     "
if [[ ! -f "${LIBDIR}/libXtst.so" ]]; then
    echo "${LIBDIR}/libXtst.so not found!"
    printf "\n ERROR: Install libXtst-devel ";bitness
    show_help
    exit 1
fi; echo "(OK)"

# generate Makefile
sed "s#CONFIGLIBS#${LIBDIR}#" Makefile.in > Makefile
if [[ $is64 ]]; then
    if [[ ! -d "$(winepath "C:\\Program Files (x86)")" ]]; then
        if [[ -d "$(winepath "C:\\Program Files")" ]]; then
            echo "WARNING: This isn't a 64 bit WINEPREFIX!"
            echo "Might work though, with wine < 1.1 ..."
            echo "Or just delete $WINEPREFIX and run winecfg."
        else
            # create WINEPREFIX
            winecfg
        fi
    fi
    sed -i 's/Files (x86)\\/Files\\/' main.h
    sed -i 's/Files (x86)\\/Files\\/' install.sh
    echo "Generating 64 bit Makefile..."
    sed -i 's/-m32//' Makefile
else
    echo "Generating 32 bit Makefile..."
    if [[ -d "$(winepath "C:\\Program Files (x86)")" ]]; then
        sed -i 's/Files\\/Files (x86)\\/' main.h
        sed -i 's/Files\\/Files (x86)\\/' install.sh
    else
        sed -i 's/Files (x86)\\/Files\\/' main.h
        sed -i 's/Files (x86)\\/Files\\/' install.sh
    fi
fi
sed -i "s.CONFIGWINELIBS.${WINELIBDIR}." Makefile
if [[ ${XINCDIR}=="/usr/include" ]]; then
    sed -i "s.CONFIGOPTINC.." Makefile
else
    sed -i "s.CONFIGOPTINC.-I${XINCDIR}." Makefile
fi
echo "Done."
echo "Run make && make install to finish installation."
exit 0
